package {
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import com.bases.ScollBar.TControlScrollBar;
	import com.bases.TUIComponent;
	import com.skin.SkinManage;
	import com.xjw.adapte.AdapteManager;
	import com.xjw.adapte.IAdapte;
	
	import config.TConfig;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	import manager.server.Server;
	import manager.server.factory.Commands;
	import manager.server.factory.LoginCommand;
	import manager.webservice.IWebServices;
	import manager.webservice.WebServiceControl;
	import manager.xml.XMLConfig;
	
	[SWF(width="800",height="600")]
	public class FlashBase extends TUIComponent implements IWebServices,IAdapte {
		private var mc:MovieClip;
		private var bulk:BulkLoader;
		
		public function FlashBase() {
			/*var byte:ByteArray = new ByteArray();
			byte.writeUTFBytes("系信");
			
			byte.position = 0;
			while(byte.bytesAvailable > 0) {
				trace(byte.readByte().toString(16));
			}*/
			//trace(byte.readUTFBytes(byte.length));
			//socketHandler();
			//var byte:ByteArray = StrBytes.toByteArray("aabbc")
			//trace(StrBytes.toString(byte));
			
			//addChild(new FontJSON);
			webHttp();
		}
		
		private function webHttp():void{
			var request:URLRequest = new URLRequest();
			request.url = "http://server.lezone.co/xmppservice.asmx/Login?"
			request.method = URLRequestMethod.POST;
			
			var variables:URLVariables = new URLVariables();
			variables.username = "591400";
			variables.pwd = "123456";
			request.data = variables;
			
			var url:URLLoader = new URLLoader();
			url.addEventListener(Event.COMPLETE,comHandler);
			url.addEventListener(IOErrorEvent.IO_ERROR,errHandler);
			url.load(request)
		}
		private function comHandler(e:Event):void{
			trace(XML(URLLoader(e.target).data))
		}
		private function errHandler(e:IOErrorEvent):void{
			trace(e)
		}
		private function createShape($parent:DisplayObjectContainer=null):Shape{
			var __shape:Shape = new Shape();
			//__shape.graphics.lineStyle(2, 0xFF0000);
			__shape.graphics.beginFill(0xFF0000);
			__shape.graphics.drawRect(0, 0 , 30, 30);
			if($parent)
			 $parent.addChild(__shape);
			 return __shape;
        }

		private var socket:Server;
		private function socketHandler():void{
			var sprite:Sprite = new Sprite;
			sprite.graphics.beginFill(0xff0000,1);
			sprite.graphics.drawRect(0,0,300,268);
			sprite.graphics.endFill();
			this.addChild(sprite);
			
			socket = new Server("localhost",8888);
			socket.connect();
			sprite.addEventListener(MouseEvent.CLICK,sendManager);
		}
		/**触发发送事件*/
		private function sendManager(e:MouseEvent):void{
			var login:LoginCommand = new LoginCommand(Commands.LOGIN,"系统","admin");
			var bytes:ByteArray = login.byteArray;
			
			socket.send(bytes,backHandler);
		}
		/**回调函数*/
		private function backHandler(obj:LoginCommand):void{
			trace(obj.userName,obj.userID);
		}
		
		
		private function loadHandler(val:String,i:int):void{
			var loader:Loader = new Loader();
			//loader.contentLoaderInfo.addEventListener(Event.COMPLETE,comple);
			loader.load(new URLRequest(val));
			loader.x = (i<5)?i * 120:(i-5)*120;
			loader.y = (i<5)?0:164;
			this.addChild(loader);
		}
		
		/**
		 * 设置初始舞台 
		 */		
		private  function setStageInit():void{
			AdapteManager.getInstance().setStage(this.stage);
			TConfig.getInstance().initStage(this.stage); 
			TConfig.getInstance().mainSprite=this;
		}
		/**
		 * 有关自适应的 
		 * @param w
		 * @param h
		 */		
		public function adapte(w:Number,h:Number):void{
			this.width = w;
			this.height= h;
		}
		private function completeHandler(e:BulkProgressEvent):void{
			SkinManage.getInstance().skinSwf = bulk.getMovieClip('base');
			var xml:XML = bulk.getXML("config");
			XMLConfig.getInstance().setXML = xml;
			
			
			WebServiceControl.getInstance().addArgumentWebServices(this,"getSupportDataSet",
				[]);
			this.relayout();
		}
		private function handler(e:Event):void{
			XMLConfig.getInstance().setXML = XML(e.target.data);
			trace(XMLConfig.getInstance().key,XMLConfig.getInstance().wsdl);
		}
		
		override protected function relayout():void{
			super.relayout();
		}
		//实现Web的接口方法
		public function updateData(value:*):void{
			trace(value.toString())
		}
		
		private function callBack(value:*):void{
			trace(value.toString())
		}
		
		
		/**
		 * 滚动条 
		 */		
		private function scroll():void{
			var sprite:Sprite = new Sprite;
			sprite.graphics.beginFill(0xff0000,1);
			sprite.graphics.drawRect(0,0,300,268);
			sprite.graphics.endFill();
			this.addChild(sprite);
			var s:Sprite = new Sprite;
			s.graphics.beginFill(0xffffff,1);
			s.graphics.drawRect(0,0,300,468);
			s.graphics.endFill();
			s.mask = sprite;
			this.addChild(s);
			
			var txt:TextField = new TextField;
			txt.text = "文本\n\r文本\n\r文本\n\r文本\n\r文本\n\r文本\n\r文本\n\r文本\n\r文本\n\r文本\n\r文本\n\r";
			txt.y = 70;
			txt.x = 80;
			s.addChild(txt);
			
			var scroll:TControlScrollBar = new TControlScrollBar(s);
			scroll.maskHeight = 268 + 5;
			scroll.setSize(15,268);
			scroll.invalidateScrollBar();
			//this.addChild(scroll);
		}
	}
}